package com.mhkj.workspace.notify.service.impl;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.mhkj.revise.plan.entity.PlanNode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mhkj.workspace.notify.dao.NotifyDao;
import com.mhkj.workspace.notify.entity.Notify;
import com.mhkj.workspace.notify.repository.NotifyRepository;
import com.mhkj.workspace.notify.service.NotifyService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.util.UserHelper;
import com.mics.core.system.base.service.impl.BaseServiceImpl;

@Service
public class NotifyServiceImpl  extends BaseServiceImpl<Notify> implements NotifyService {
    @Resource
    private NotifyRepository notifyRepository;
    @Resource
    private NotifyDao notifyDao;
    @Resource
    public void setNotifyRepository(NotifyRepository notifyRepository) {
        super.setBaseRepository(notifyRepository);
    }

   //预警
    public Pagination<Notify> listByPage(Notify notify, int pageIndex, int size) {
        Specification<Notify> spec = new Specification<Notify> () {
            /**
             * 序列号
             */
            private static final long serialVersionUID = -786003485941679959L;
            @Override
            public Predicate toPredicate(Root<Notify> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> lstPredicates = new ArrayList<Predicate>();
                List<Integer> t = new ArrayList<Integer>();
               // t.add(5);
                t.add(6);
                t.add(7);
                Predicate p1=criteriaBuilder.and(root.get("type").in(t));
                lstPredicates.add(p1);

                String userId = UserHelper.getCurrentUserId();
                Predicate p2=criteriaBuilder.equal(root.get("acceptUserId").as(String.class), userId);
                lstPredicates.add(p2);
                List<Integer> t1 = new ArrayList<Integer>();
                t1.add(0);
                t1.add(1);
                if(notify.getEstate()==2){
                    List<Integer> t2 = new ArrayList<Integer>();
                    t2.add(0);
                    Predicate p3=criteriaBuilder.and(root.get("estate").in(t2));
                    lstPredicates.add(p3);
                }else if(notify.getEstate()==1){
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);

                }else {
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);
                }
                Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
                return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
            }
        };

        List<Notify> notifies = new ArrayList<Notify>();
        Page<Notify> pageT =  notifyDao.findAll(spec, PageRequest.of(pageIndex, size,  new Sort(Sort.Direction.DESC, "createDate").and(new Sort(Sort.Direction.ASC, "id"))));

       // Page<Notify> pageT = notifyDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.DESC, "createDate"));
        Pagination<Notify> pagination = new Pagination<Notify>();
        pagination.setTotal(notifyDao.count(spec));
        pagination.setData(pageT.getContent());
        return pagination;
    }
//汇报
    public Pagination<Notify> affectedListByPage(Notify notify, int pageIndex, int size) {
        Specification<Notify> spec = new Specification<Notify> () {
            /**
             * 序列号
             */
            private static final long serialVersionUID = -786003485941679959L;
            @Override
            public Predicate toPredicate(Root<Notify> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                notify.setType(3);
                List<Predicate> lstPredicates = new ArrayList<Predicate>();
                Predicate p1=criteriaBuilder.equal(root.get("type"), notify.getType());
                lstPredicates.add(p1);
                String userId = UserHelper.getCurrentUserId();
                Predicate p2=criteriaBuilder.equal(root.get("acceptUserId").as(String.class), userId);
                lstPredicates.add(p2);
                List<Integer> t1 = new ArrayList<Integer>();
                t1.add(0);
                t1.add(1);
                if(notify.getEstate()==2){
                    List<Integer> t2 = new ArrayList<Integer>();
                    t2.add(0);
                    Predicate p3=criteriaBuilder.and(root.get("estate").in(t2));
                    lstPredicates.add(p3);
                }else if(notify.getEstate()==1){
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);

                }else {
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);
                }
                Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
                return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
            }
        };
        Page<Notify> pageT =  notifyDao.findAll(spec, PageRequest.of(pageIndex, size,  new Sort(Sort.Direction.DESC, "createDate").and(new Sort(Sort.Direction.ASC, "id"))));
        //Page<Notify> pageT = notifyDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
        Pagination<Notify> pagination = new Pagination<Notify>();
        pagination.setTotal(notifyDao.count(spec));
        pagination.setData(pageT.getContent());
        return pagination;
    }

//考核
    public Pagination<Notify> needListByPage(Notify notify, int pageIndex, int size) {
        Specification<Notify> spec = new Specification<Notify> () {
            /**
             * 序列号
             */
            private static final long serialVersionUID = -786003485941679959L;
            @Override
            public Predicate toPredicate(Root<Notify> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                notify.setType(5);
                List<Predicate> lstPredicates = new ArrayList<Predicate>();

                Predicate p1=criteriaBuilder.equal(root.get("type"), notify.getType());
                lstPredicates.add(p1);

                String userId = UserHelper.getCurrentUserId();
                Predicate p2=criteriaBuilder.equal(root.get("acceptUserId").as(String.class), userId);
                lstPredicates.add(p2);
                List<Integer> t1 = new ArrayList<Integer>();
                t1.add(0);
                t1.add(1);
                if(notify.getEstate()==2){
                    List<Integer> t2 = new ArrayList<Integer>();
                    t2.add(0);
                    Predicate p3=criteriaBuilder.and(root.get("estate").in(t2));
                    lstPredicates.add(p3);
                }else if(notify.getEstate()==1){
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);

                }else {
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);
                }
                Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
                return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
            }
        };

     //   Page<Notify> pageT = notifyDao.findAll(spec, PageRequest.of(pageIndex, size, new Sort(Sort.Direction.DESC, "createDate")));
        Page<Notify> pageT =  notifyDao.findAll(spec, PageRequest.of(pageIndex, size,  new Sort(Sort.Direction.DESC, "createDate").and(new Sort(Sort.Direction.ASC, "id"))));
        Pagination<Notify> pagination = new Pagination<Notify>();
        pagination.setTotal(notifyDao.count(spec));
        pagination.setData(pageT.getContent());
        return pagination;
    }

    //待办

    public Pagination<Notify> confirmListByPage(Notify notify, int pageIndex, int size) {
        Specification<Notify> spec = new Specification<Notify> () {
            /**
             * 序列号
             */
            private static final long serialVersionUID = -786003485941679959L;
            @Override
            public Predicate toPredicate(Root<Notify> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                notify.setType(1);

                List<Predicate> lstPredicates = new ArrayList<Predicate>();
                List<Integer> t1 = new ArrayList<Integer>();
                t1.add(0);
                t1.add(1);
                if(notify.getEstate()==2){
                    List<Integer> t2 = new ArrayList<Integer>();
                    t2.add(0);
                    Predicate p3=criteriaBuilder.and(root.get("estate").in(t2));
                    lstPredicates.add(p3);
                }else if(notify.getEstate()==1){
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);

                }else {
                    Predicate p3 = criteriaBuilder.and(root.get("estate").in(t1));
                    lstPredicates.add(p3);
                }
               Predicate p1=criteriaBuilder.equal(root.get("type"), notify.getType());
                lstPredicates.add(p1);
                String userId = UserHelper.getCurrentUserId();
                Predicate p2=criteriaBuilder.equal(root.get("acceptUserId").as(String.class), userId);
                lstPredicates.add(p2);
                Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
                return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
            }
        };
        Page<Notify> pageT =  notifyDao.findAll(spec, PageRequest.of(pageIndex, size,  new Sort(Sort.Direction.DESC, "createDate").and(new Sort(Sort.Direction.ASC, "id"))));
       // Page<Notify> pageT = notifyDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
        Pagination<Notify> pagination = new Pagination<Notify>();
        pagination.setTotal(notifyDao.count(spec));
        pagination.setData(pageT.getContent());
        return pagination;
    }
}
